window.onload = function(){
    //登录
    var loginBtn=document.getElementById("loginBtn");
    var user_name=document.getElementById("user_name");
    var user_password=document.getElementById("user_password");
    loginBtn.onclick=function(){
        var Us=user_name.value;
        var Ps=user_password.value;
        $.ajax({
            url:"http://118.195.129.130:3000/user/login",
            type:"post",
            async:true,
            data:{
                us: Us,
                ps:Ps
            },
            success:function(data){
                console.log(data);
                if(data.err==0){
                    alert(data.msg);
                    window.location.href="../index.html";
                    var login_id = data.data[0]._id;
                    localStorage.setItem("login_id",login_id);
                    console.log(data.data._id);
                }
                if(data.err==-1){
                    alert("用户名或密码错误")
                }
            },
            error:function(err) {
                console.log(err);
            }
        })
    }
    //注册的弹窗
    var register=document.getElementById("register");
    var popBox=document.getElementById("popBox");
    var popLayer=document.getElementById("popLayer");
    var closeBox=document.getElementById("closeBox");
    register.onclick=function(){
        popBox.style.display="block";
        popLayer.style.display="block";
    }
    closeBox.onclick=function() {
        popBox.style.display="none";
        popLayer.style.display="none";
    }
    //注册
    //获取验证码
    var send=document.getElementById("send");
    var email=document.getElementById("email");
    send.onclick=function(){
        Email=email.value;
        $.ajax({
            url:"http://118.195.129.130:3000/user/getMailCode",
            type:"post",
            data:{
                mail:Email
            },
            success:function(data){
                console.log(data);
            },
            error:function(err){
                console.log(err);
            }
        })
    }
    //注册
    var registerBtn=document.getElementById("registerBtn");
    var reUserName=document.getElementById("reUserName");
    var reUserPassword=document.getElementById("reUserPassword");
    var code=document.getElementById("code");
    registerBtn.onclick=function(){
        var us=reUserName.value;
        var ps=reUserPassword.value;
        var mail=email.value;
        var Code=code.value;
        $.ajax({
            url:"http://118.195.129.130:3000/user/reg",
            type:"post",
            data:{
                us:us,
                ps:ps,
                mail:mail,
                code:Code
            },
            success:function(data){
                console.log(data);
                if(data.err==0){
                    window.location.href="../index.html";
                }
                if(data.err==-1){
                    alert("验证码或密码错误");
                }
            },
            error:function(err){
                console.log(err);
            }
        })
    }

}